package cn.edu.sgu.www.mhxysy.controller.chongwu;

import cn.edu.sgu.www.mhxysy.consts.CustomConstants;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.consts.MimeTypes;
import cn.edu.sgu.www.mhxysy.dto.chongwu.ChongwuCategoryDTO;
import cn.edu.sgu.www.mhxysy.entity.chongwu.ChongwuCategory;
import cn.edu.sgu.www.mhxysy.pager.chongwu.ChongwuCategoryPager;
import cn.edu.sgu.www.mhxysy.restful.JsonPage;
import cn.edu.sgu.www.mhxysy.restful.JsonResult;
import cn.edu.sgu.www.mhxysy.service.chongwu.ChongwuCategoryService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "宠物类型管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/chongwu_category", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class ChongwuCategoryController {

	private final ChongwuCategoryService chongwuCategoryService;

	@Autowired
	public ChongwuCategoryController(ChongwuCategoryService chongwuCategoryService) {
		this.chongwuCategoryService = chongwuCategoryService;
	}

	@ApiOperation("上传图片")
	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	public JsonResult<String> upload(MultipartFile file) throws IOException {
		String url = chongwuCategoryService.upload(file);

		return JsonResult.success(CustomMessages.UPLOAD_SUCCESS, url);
	}

	@ApiOperation("添加宠物类型")
	@ApiOperationSupport(ignoreParameters = "id")
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public JsonResult<Void> insert(@Validated ChongwuCategoryDTO chongwuCategoryDTO) {
		chongwuCategoryService.insert(chongwuCategoryDTO);

		return JsonResult.success(CustomMessages.INSERT_SUCCESS);
	}

	@ApiOperation("通过ID修改宠物类型信息")
	@RequestMapping(value = "/updateById", method = RequestMethod.POST)
	public JsonResult<Void> updateById(@Validated ChongwuCategoryDTO chongwuCategoryDTO) {
		chongwuCategoryService.updateById(chongwuCategoryDTO);

		return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
	}

	@ApiOperation("通过ID修改宠物图片")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "宠物类型ID", required = true),
			@ApiImplicitParam(name = "image", value = "图片地址", required = true)
	})
	@RequestMapping(value = "/updateImageById", method = RequestMethod.POST)
	public JsonResult<Void> updateImageById(
			@NotNull(message = CustomMessages.ID_NOT_NULL) Integer id,
			@NotBlank(message = CustomMessages.IMAGE_NOT_NULL) String image) {
		chongwuCategoryService.updateImageById(id, image);

		return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
	}

	@ApiOperation("查询全部宠物类型")
	@RequestMapping(value = "/selectAll", method = RequestMethod.GET)
	public JsonResult<List<ChongwuCategory>> selectAll() {
		List<ChongwuCategory> list = chongwuCategoryService.selectAll();

		return JsonResult.success(null, list);
	}

	@ApiOperation("查询特殊宠物类型列表")
	@RequestMapping(value = "/selectSpecial", method = RequestMethod.GET)
	public JsonResult<List<ChongwuCategory>> selectSpecial() {
		List<ChongwuCategory> list = chongwuCategoryService.selectSpecial();

		return JsonResult.success(null, list);
	}

	@ApiOperation("通过ID查询宠物类型")
	@RequestMapping(value = "/selectById", method = RequestMethod.GET)
	public JsonResult<ChongwuCategory> selectById(
			@ApiParam(name = "id", value = "宠物类型ID", required = true)
			@NotNull(message = CustomMessages.ID_NOT_NULL) Integer id) {
		ChongwuCategory chongwuCategory = chongwuCategoryService.selectById(id);

		return JsonResult.success(null, chongwuCategory);
	}

	@ApiOperation("分页查询宠物类型列表")
	@RequestMapping(value = "/selectByPage", method = RequestMethod.GET)
	public JsonResult<JsonPage<ChongwuCategory>> selectByPage(ChongwuCategoryPager pager) {
		Page<ChongwuCategory> result = chongwuCategoryService.selectByPage(pager);

		return JsonResult.restPage(result);
	}

	@ApiOperation("查询指定等级范围的宠物类型列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "min", value = "等级下限", required = true),
			@ApiImplicitParam(name = "max", value = "等级上限", required = true)
	})
	@RequestMapping(value = "/selectByGradeRange", method = RequestMethod.GET)
	public JsonResult<List<ChongwuCategory>> selectByGradeRange(
			@NotNull(message = "等级下限" + CustomMessages.NOT_NULL) Integer min,
			@NotNull(message = "等级上限" + CustomMessages.NOT_NULL)Integer max) {
		List<ChongwuCategory> list = chongwuCategoryService.selectByGradeRange(min, max);

		return JsonResult.success(null, list);
	}

}